package com.lucweb.action;



import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lucweb.entity.BaseDict;
import com.lucweb.entity.TaskGz;
import com.lucweb.entity.TaskQuestion;
import com.lucweb.service.BaseDictService;
import com.lucweb.service.QuestionService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller("questionAction")
@Scope("prototype")
public class QuestionAction extends ActionSupport{

    @Resource
    private QuestionService questionService;
    @Resource
    private BaseDictService baseDictService;

    private static final long serialVersionUID = 1L;



    private String company;

    private String type;

    private String isfinish;

    private int questionId;

    private int quesNew;



    private TaskGz taskGz;


    private TaskQuestion taskQuestion;


    public List<TaskQuestion> getQuestionList1() {
        return questionList1;
    }

    public void setQuestionList1(List<TaskQuestion> questionList1) {
        this.questionList1 = questionList1;
    }

    private List<TaskQuestion>questionList1 = new ArrayList<TaskQuestion>();




    //添加和更新问题
    public String addQuestion() throws IOException {


        if(taskQuestion.getNeedcl()==null){
            taskQuestion.setNeedcl("0");
        }
        if(taskQuestion.getCompanyName()==null){
            taskQuestion.setCompanyName(company);
        }

        System.out.println(taskQuestion+"fasdfadsfasd");
        boolean flag = questionService.addQuestion(taskQuestion);

        System.out.println(flag+"fasdfadsfasd");
        String json = JSON.toJSONString("添加问题成功！！" +
                "", SerializerFeature.WriteNullStringAsEmpty);

        //3 将json发送给浏览器

        ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
        ServletActionContext.getResponse().getWriter().write(json);


        return null;
    }

    //删除问题
    public String deleteQuestion(){

        boolean flag = questionService.deleteQuestion(questionId);

        ActionContext.getContext().put("company", company);

        return null;
    }


    //新建问题
    public String newQuestion(){
        System.out.println("compandsjhfkajsd"+taskQuestion);

        if(quesNew==1){


            int bid = questionService.getmaxId();

            List<BaseDict>nameList=baseDictService.getListByTypeCode("3");
            ActionContext.getContext().put("nameList",nameList);

            ActionContext.getContext().put("company", company);
            ActionContext.getContext().put("type", type);

            ActionContext.getContext().put("bid", bid);

            return  "editQuestionNew";
        }else {

            int bid = questionService.getmaxId();

            List<BaseDict>nameList=baseDictService.getListByTypeCode("3");
            ActionContext.getContext().put("nameList",nameList);

            ActionContext.getContext().put("company", company);
            ActionContext.getContext().put("type", type);

            ActionContext.getContext().put("bid", bid);
            return "editQuestion";

        }


    }


    //打印问题
    public String printQuestion(){


        List<TaskQuestion>questionList = questionService.getQuestion("companyName = '"+company+"' " +
               "and isfinish != '已处理' and type = '"+type+"'");


        ActionContext.getContext().put("questionList", questionList);
        ActionContext.getContext().put("company", company);
        ActionContext.getContext().put("type", type);



        return "printQues";
    }

    //打印问题
    public String printQuestionycl(){


        List<TaskQuestion>questionList = questionService.getQuestion("companyName = '"+company+"' " +
                "and isfinish = '已处理' and type = '"+type+"'");


        ActionContext.getContext().put("questionList", questionList);
        ActionContext.getContext().put("company", company);
        ActionContext.getContext().put("type", type);



        return "printQues";
    }


    public String bcyjljwtQuestion(){


        List<TaskQuestion>questionList = questionService.getQuestion("companyName = '"+company+"' " +
                "and ( lx = '合同遗留' or gf = '甲方处理' or gf = '甲供材料' )");


        ActionContext.getContext().put("questionList1", questionList);
        ActionContext.getContext().put("company", company);



        return "bcyjljwtQuestion";
    }

    public String printbcyjljwtQuestion(){

        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }


        List<TaskQuestion>questionList = questionService.getQuestion("companyName = '"+company+"' " +
                "and ( lx = '合同遗留' or gf = '甲方处理' or gf = '甲供材料' )  and isSelected ='1'");


        ActionContext.getContext().put("questionList", questionList);
        ActionContext.getContext().put("company", company);

        ActionContext.getContext().put("taskGz", taskGz);


        return "printbcyjljwtQuestion";
    }





    //打印问题
    public String printQuestionfk(){

        System.out.println("aaaaaaaaaaaa"+questionList1);
        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }





        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and (type = '维护' or type = '维护遗留' ) and isSelected ='1'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);

        ActionContext.getContext().put("taskGz", taskGz);


        return "printQuesfk";
    }

    //打印问题
    public String printQuestionBj(){

        System.out.println("aaaaaaaaaaaa"+questionList1);
        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护遗留'  and isSelected ='1'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);

        ActionContext.getContext().put("taskGz", taskGz);


        return "printQuesBj";
    }


    //打印已处理问题
    public String printQuestionYcl(){

        System.out.println("aaaaaaaaaaaa"+questionList1);
        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and isfinish = '已处理'  and isSelected ='2'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);

        ActionContext.getContext().put("taskGz", taskGz);


        return "printQuesYcl";
    }

    public String printQuestioncgBj(){

        System.out.println("aaaaaaaaaaaa"+questionList1);
        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护'  and isSelected ='1'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);

        ActionContext.getContext().put("taskGz", taskGz);


        return "printQuescgBj";
    }


    public String printQuestioncgSq(){


        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护'  and isSelected ='1'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);

        ActionContext.getContext().put("taskGz", taskGz);


        return "printQuescgSq";
    }


    public String printQuestioncgHz(){

        System.out.println("aaaaaaaaaaaa"+questionList1);
        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护'  and isSelected ='1'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);

        ActionContext.getContext().put("taskGz", taskGz);


        return "printQuescgHz";
    }

    public String printQuestioncgGz(){

        System.out.println("aaaaaaaaaaaa"+questionList1);
        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护'  and isSelected ='1'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);

        ActionContext.getContext().put("taskGz", taskGz);


        return "printQuescgGz";
    }


    //打印问题
    public String printQuestionGz(){

        System.out.println("aaaaaaaaaaaa"+questionList1);
        for(int i=0; i<questionList1.size();i++){
            taskQuestion=questionList1.get(i);

            questionService.addQuestion(taskQuestion);

        }



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护遗留'  and isSelected ='1'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);

        ActionContext.getContext().put("taskGz", taskGz);


        return "printQuesGz";
    }




    //打印问题
    public String printQuestionAll(){



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' and isfinish != '已处理'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);


        return "printQuesAll";
    }

    //打印问题
    public String printQuestionAllycl(){



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' and isfinish = '已处理'");

        ActionContext.getContext().put("questionListAll", questionListAll);
        ActionContext.getContext().put("company", company);


        return "printQuesAll";
    }

    //打印问题
    public String whyjylQuestion(){



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
        "and (type = '维护' or type = '维护遗留' )");


        ActionContext.getContext().put("questionList1", questionListAll);
        ActionContext.getContext().put("company", company);


        return "whyjylQuestion";
    }




    //打印问题
    public String whyjcgGzQuestion(){



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护' ");


        ActionContext.getContext().put("questionList1", questionListAll);
        ActionContext.getContext().put("company", company);


        return "whyjcgGzQuestion";
    }



    public String whyjcgBjQuestion(){



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护' ");


        ActionContext.getContext().put("questionList1", questionListAll);
        ActionContext.getContext().put("company", company);


        return "whyjcgBjQuestion";
    }

    public String whyjcgSqQuestion(){



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护' ");


        ActionContext.getContext().put("questionList1", questionListAll);
        ActionContext.getContext().put("company", company);


        return "whyjcgSqQuestion";
    }

    public String whyjcgHzQuestion(){



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and type = '维护' ");


        ActionContext.getContext().put("questionList1", questionListAll);
        ActionContext.getContext().put("company", company);


        return "whyjcgHzQuestion";
    }


    //打印问题
    public String whyjylBjQuestion(){


        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and  type = '维护遗留' ");


        ActionContext.getContext().put("questionList1", questionListAll);

        ActionContext.getContext().put("company", company);


        return "whyjylBjQuestion";
    }

    public String whyjylGzQuestion(){


        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and  type = '维护遗留' ");


        ActionContext.getContext().put("questionList1", questionListAll);

        ActionContext.getContext().put("company", company);


        return "whyjylGzQuestion";
    }

    //打印问题
    public String whyjYclQuestion(){



        List<TaskQuestion>questionListAll = questionService.getQuestion("companyName ='"+company+"' "+
                "and isfinish = '已处理'");


        ActionContext.getContext().put("questionList1", questionListAll);
        ActionContext.getContext().put("company", company);


        return "whyjYclQuestion";
    }

    //修改问题
    public String editQuestion(){


        taskQuestion = questionService.getQuestionById(questionId);
        List<BaseDict>nameList=baseDictService.getListByTypeCode("3");

        ActionContext.getContext().put("nameList",nameList);

        ActionContext.getContext().put("taskQuestion", taskQuestion);

        System.out.println(taskQuestion);

        return "editQuestion";
    }


    //获取所有问题方法
    public String getQuestions() throws IOException {

        if(type.equals("维护")||type.equals("维护遗留")){
            List<TaskQuestion> questionList= questionService.getQuestion("companyName = '"+company+"' and (needcl!='1' or needcl = null ) and isfinish != '已处理' and type = '"+type+"'");
            ActionContext.getContext().put("questionList", questionList);
            //2 将list转换为 json格式
            String json = JSON.toJSONString(questionList, SerializerFeature.WriteNullStringAsEmpty);

            //3 将json发送给浏览器

            ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
            ServletActionContext.getResponse().getWriter().write(json);
        }else {
            List<TaskQuestion> questionList= questionService.getQuestion("companyName = '"+company+"' and isfinish != '已处理' and type = '"+type+"'");
            ActionContext.getContext().put("questionList", questionList);
            //2 将list转换为 json格式
            String json = JSON.toJSONString(questionList, SerializerFeature.WriteNullStringAsEmpty);

            //3 将json发送给浏览器

            ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
            ServletActionContext.getResponse().getWriter().write(json);

        }






        return null;//告诉struts2不需要进行结果处理

        //return "question";

    }

    //获取所有问题方法
    public String getQuestionList() throws IOException {


        System.out.println(isfinish);
        if(isfinish!=null&&isfinish.equals("已处理")){
            isfinish = "= '已处理'";
        }else {
            isfinish = "!= '已处理'";
        }


        List<TaskQuestion> questionList= questionService.getQuestion("companyName = '"+company+"' and type = '"+type+"' and isfinish "+isfinish);

        ActionContext.getContext().put("questionList", questionList);
        //2 将list转换为 json格式
        String json = JSON.toJSONString(questionList,SerializerFeature.WriteNullStringAsEmpty);

        //3 将json发送给浏览器

        ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
        ServletActionContext.getResponse().getWriter().write(json);


        return null;//告诉struts2不需要进行结果处理

        //return "question";

    }

    //获取所有问题方法
    public String getQuestionListycl() throws IOException {




        List<TaskQuestion> questionList= questionService.getQuestion("companyName = '"+company+"' and type = '"+type+"' and isfinish = '已处理'" );

        ActionContext.getContext().put("questionList", questionList);
        //2 将list转换为 json格式
        String json = JSON.toJSONString(questionList,SerializerFeature.WriteNullStringAsEmpty);

        //3 将json发送给浏览器

        ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
        ServletActionContext.getResponse().getWriter().write(json);


        return null;//告诉struts2不需要进行结果处理

        //return "question";

    }


    //获取所有问题方法
    public String getQuestionListAll() throws IOException {
        System.out.println(isfinish);

        if(isfinish!=null&&isfinish.equals("已处理")){
            isfinish = "= '已处理'";
        }else {
            isfinish = "!= '已处理'";
        }


        List<TaskQuestion> questionList= questionService.getQuestion("companyName = '"+company+"' and isfinish "+isfinish);

        ActionContext.getContext().put("questionList", questionList);
        //2 将list转换为 json格式
        String json = JSON.toJSONString(questionList,SerializerFeature.WriteNullStringAsEmpty);

        //3 将json发送给浏览器

        ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
        ServletActionContext.getResponse().getWriter().write(json);


        return null;//告诉struts2不需要进行结果处理

        //return "question";

    }

    //获取所有问题方法
    public String getQuestionListAllycl() throws IOException {


        List<TaskQuestion> questionList= questionService.getQuestion("companyName = '"+company+"' and isfinish = '已处理'");

        ActionContext.getContext().put("questionList", questionList);
        //2 将list转换为 json格式
        String json = JSON.toJSONString(questionList,SerializerFeature.WriteNullStringAsEmpty);

        //3 将json发送给浏览器

        ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
        ServletActionContext.getResponse().getWriter().write(json);


        return null;//告诉struts2不需要进行结果处理

        //return "question";

    }


    //获取所有问题方法
    public String showQuestions() throws IOException {
//        System.out.println("aaaaaaaaaaa"+company);
//
//        List<TaskQuestion> questionList1= questionService.getQuestion(company,"检测任务");
//        List<TaskQuestion> questionList2= questionService.getQuestion(company,"维护");
//        List<TaskQuestion> questionList3= questionService.getQuestion(company,"月检测");
//        List<TaskQuestion> questionList4= questionService.getQuestion(company,"维修任务");
//        List<TaskQuestion> questionList5= questionService.getQuestion(company,"维护遗留");
//
//        //历史遗留问题
//
//        ActionContext.getContext().put("questionList1", questionList1);
//        ActionContext.getContext().put("questionList2", questionList2);
//        ActionContext.getContext().put("questionList3", questionList3);
//        ActionContext.getContext().put("questionList4", questionList4);
//        ActionContext.getContext().put("questionList5", questionList5);


//        getques1($("#tablebody1"),company,'检测任务','未处理');
//        getquesall($("#tablebody2"),company,'未处理');
//        getques1($("#tablebody3"),company,'维护','未处理');
//        getques1($("#tablebody4"),company,'维修任务','未处理');
//        getques1($("#tablebody5"),company,'维护遗留','未处理');




        if(isfinish!=null&&isfinish.equals("已处理")){
            List<TaskQuestion> questionListAll= questionService.getQuestion("companyName = '"+company+"' and isfinish = '已处理'");
            List<TaskQuestion> questionListJc= questionService.getQuestion("companyName = '"+company+"' and type = '检测任务' and isfinish = '已处理'" );
            List<TaskQuestion> questionListWh= questionService.getQuestion("companyName = '"+company+"' and type = '维护' and isfinish = '已处理'" );
            List<TaskQuestion> questionListWx= questionService.getQuestion("companyName = '"+company+"' and type = '维修任务' and isfinish = '已处理'" );
            List<TaskQuestion> questionListWhyl= questionService.getQuestion("companyName = '"+company+"' and type = '维护遗留' and isfinish = '已处理'" );


            ActionContext.getContext().put("questionListAll", questionListAll);
            ActionContext.getContext().put("questionListJc", questionListJc);
            ActionContext.getContext().put("questionListWh", questionListWh);
            ActionContext.getContext().put("questionListWx", questionListWx);
            ActionContext.getContext().put("questionListWhyl", questionListWhyl);
            ActionContext.getContext().put("company", company);
            ActionContext.getContext().put("isfinish", isfinish);





            return "questionDataycl";//告诉struts2不需要进行结果处理
        }else {


            List<TaskQuestion> questionListAll= questionService.getQuestion("companyName = '"+company+"' and isfinish != '已处理'");
            List<TaskQuestion> questionListJc= questionService.getQuestion("companyName = '"+company+"' and type = '检测任务' and isfinish != '已处理'" );
            List<TaskQuestion> questionListWh= questionService.getQuestion("companyName = '"+company+"' and type = '维护' and isfinish != '已处理'" );
            List<TaskQuestion> questionListWx= questionService.getQuestion("companyName = '"+company+"' and type = '维修任务' and isfinish != '已处理'" );
            List<TaskQuestion> questionListWhyl= questionService.getQuestion("companyName = '"+company+"' and type = '维护遗留' and isfinish != '已处理'" );


            ActionContext.getContext().put("questionListAll", questionListAll);
            ActionContext.getContext().put("questionListJc", questionListJc);
            ActionContext.getContext().put("questionListWh", questionListWh);
            ActionContext.getContext().put("questionListWx", questionListWx);
            ActionContext.getContext().put("questionListWhyl", questionListWhyl);
            ActionContext.getContext().put("company", company);
            ActionContext.getContext().put("isfinish", isfinish);





            return "questionData";//告诉struts2不需要进行结果处理
        }



        //return "question";

    }



    public void setCompany(String company) {
        this.company = company;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setIsfinish(String isfinish) {
        this.isfinish = isfinish;
    }


    public TaskQuestion getTaskQuestion() {
        return taskQuestion;
    }

    public void setTaskQuestion(TaskQuestion taskQuestion) {
        this.taskQuestion = taskQuestion;
    }


    public void setQuestionId(int questionId) {
        this.questionId = questionId;
    }
    public TaskGz getTaskGz() {
        return taskGz;
    }

    public void setTaskGz(TaskGz taskGz) {
        this.taskGz = taskGz;
    }


    public int getQuesNew() {
        return quesNew;
    }

    public void setQuesNew(int quesNew) {
        this.quesNew = quesNew;
    }
}
